﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Barebones.Components;
using Meat.Input;
using Barebones.Dependencies;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace _2dgame.Components
{
    class ControlComponent : EntityComponent, Barebones.Framework.IUpdateable
    {
        KeyboardReader m_Keyboard;
        PhysicsComponent m_Physics;

        float m_Speed;

        public ControlComponent(float speed)
        {
            m_Speed = speed;
        }

        public override IEnumerable<Barebones.Dependencies.IDependency> GetDependencies()
        {
            yield return new Dependency<KeyboardReader>(item => m_Keyboard = item);
            yield return new Dependency<PhysicsComponent>(item => m_Physics = item);
        }

        public void Update(float dt)
        {
            Vector2 vel = m_Physics.LinearVelocity;
            vel.X = 0;
            vel.Y = 0;

            if (m_Keyboard.IsKeyDown(Keys.Right))
                vel.X += m_Speed;

            if (m_Keyboard.IsKeyDown(Keys.Left))
                vel.X -= m_Speed;

            if (m_Keyboard.IsKeyDown(Keys.Down))
                vel.Y -= m_Speed;

            if (m_Keyboard.IsKeyDown(Keys.Up))
                vel.Y += m_Speed;

            m_Physics.LinearVelocity = vel;
        }
    }
}
